package ert;

import robocode.HitWallEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import java.awt.Color;

/**
 * RiceMusubi
 * 
 * A robot that moves parallel to its enemy while firing at it. It does some random movement to get
 * away from the corner to avoid getting cornered.
 * 
 * @author Emerson Tabucol
 * 
 */
public class RiceMusubi extends Robot {

  boolean hitWall = false;
  double degree = 90;


  /**
   * Run method is called at the start of the game.
   */
  public void run() {

    // Set robot colors
    setBodyColor(Color.black);
    setGunColor(Color.black);
    setRadarColor(Color.black);
    setBulletColor(Color.red);

    // Make sure that the radar can turn independently
    setAdjustRadarForRobotTurn(true);
    setAdjustGunForRobotTurn(true);

    // Look for other robots while moving forward
    while (true) {
      turnRadarRight(360);
      ahead(150);
    }
  }

  /**
   * onScannedRobot is called by when other robot is scanned.
   * 
   * @param e The scanned robot.
   */
  public void onScannedRobot(ScannedRobotEvent e) {
    // Call myFire method. See method MyFire method.
    myFire(e);
    // If it did not hit any wall
    if (hitWall) {
      // move parallel to the enemy
      turnRight(e.getBearing() - degree);
      // and keep the gun pointing at it
      turnGunRight(getHeading() - getGunHeading() + e.getBearing());
    }
    else {
      // change degree from 90 to -90 and vice versa when it hits a wall
      degree *= -1;
      // move parallel to the other robot
      turnRight(e.getBearing() - degree);
      // and keep gun pointing at the enemy
      turnGunRight(getHeading() - getGunHeading() + e.getBearing());
      hitWall = false; // reset the hitWall
    }
    scan();
  }

  /**
   * myFire method calls fire() with fire power as parameter. It adjust the firepower depending on
   * the other robot's distance.
   * 
   * @param e The scanned robot
   */
  public void myFire(ScannedRobotEvent e) {

    // If the other robot is near
    if (e.getDistance() <= 100) {

      // Use full power
      fire(3);
    }
    else if (e.getDistance() >= 100 && e.getDistance() <= 200) {
      // Use lesser bullet power
      fire(2);
    }
    else {
      // Use the least power
      fire(1);
    }
  }

  /**
   * onHitWall is called when the robots hit the wall.
   * 
   * @param e The event it hits the wall
   */
  public void onHitWall(HitWallEvent e) {
    // get the batllefield's width and height
    double xBattleField = getBattleFieldWidth();
    double yBattleField = getBattleFieldHeight();
    // Checks if the robot is in any of the corner
    if (((getX() <= 100) && (getY() <= 100)) || ((getX() > xBattleField - 100) && (getY() <= 100))
        || ((getX() <= 100) && (getY() > yBattleField - 100))
        || ((getX() > xBattleField - 100) && (getY() > yBattleField - 100))) {

      // do some move and get out of the corner
      //turnLeft(45);
      //ahead(300);
      leaveCorner();
      hitWall = false;
    }
    else {

      hitWall = true;
    }
  }

  /**
   * LeaveCorner is adapted from the Position04 robot. It uses pythagorean theorem
   * to get the robot out of the corner.
   */
  public void leaveCorner() {
    // Get the center of the battlefield
    double xCenter = getBattleFieldWidth() / 2;
    double yCenter = getBattleFieldHeight() / 2;
    // Get the position of the robot and subtract from the given x & y coordinates
    double xDistance = Math.abs(xCenter - getX());
    double yDistance = Math.abs(yCenter - getY());
  //Use Pythagorean theorem to find the angle to the destination
    double angle = Math.atan(xDistance / yDistance) * (180 / Math.PI);
    double heading = getHeading();
    //Use Pythagorean theorem to compute the distance to the destination
    double distance = Math.sqrt((xDistance * xDistance) + (yDistance * yDistance));  

    // if robot is in the lower left corner
    if ((getX() < xCenter) && (getY() < yCenter)) {
      // Get out of the corner and then some
      turnLeft(heading - angle);
      ahead(distance);
      //turnLeft(45);
      //back(200);

      // if robot is in lower right corner
    }
    else if ((getX() > xCenter) && (getY() < yCenter)) {
      // Get out of the corner and then some
      turnLeft(heading + angle);
      ahead(distance);
      //turnLeft(45);
      //back(200);
      // if robot is in the upper left side
    }
    else if ((getX() < xCenter) && (getY() > yCenter)) {
      // Get out of the corner and then some
      turnLeft(heading - 180 + angle);
      ahead(distance);
      //turnLeft(45);
      //back(200);
    }
    else {
      // Get out of the corner and then some
      turnLeft(heading - 180 - angle);
      ahead(distance);
      //turnLeft(45);
      //back(200);
    }
  }
} // end class
